package com.zrt.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrt.springboot.common.Result;
import com.zrt.springboot.entity.SensorRegistered;
import com.zrt.springboot.service.ISensorRegisteredService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 青哥哥
 * @since 2023-03-11
 */
@RestController
@RequestMapping("/sensor-registered")
public class SensorRegisteredController {

    @Resource
    private ISensorRegisteredService sensorRegisteredService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody SensorRegistered sensorRegistered) {
            return Result.success(sensorRegisteredService.saveOrUpdate(sensorRegistered));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
            return Result.success(sensorRegisteredService.removeById(id));
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
            return Result.success(sensorRegisteredService.removeByIds(ids));
    }

    @GetMapping("/findAllSensorId")
    public Result findAllSensorId() {

        List<Object> objects = sensorRegisteredService.listObjs(new QueryWrapper<SensorRegistered>().lambda().select(SensorRegistered::getSensorId),Object::toString);
        System.out.println("================>findAllSensorId");
        System.out.println(objects);
        return Result.success(objects);
    }

    @GetMapping("/findAll")
    public Result findAll() {
            System.out.println("findAll...............");
            return Result.success(sensorRegisteredService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
            return Result.success(sensorRegisteredService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize
//                           @RequestParam(defaultValue = "") Integer sensorId
    ){
            System.out.println("SensorRegisteredController*************************************");
            System.out.println("SensorRegisteredController*************************************");
            QueryWrapper<SensorRegistered> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByAsc("id");
//            queryWrapper.like("sensor_id", sensorId);
            return Result.success(sensorRegisteredService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


}

